package com.hundsun.cprs.yyt.web.action.system;

import com.hundsun.cprs.yyt.common.enums.EnumBusinessCode;
import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.service.system.service.ClearingTradeService;
import com.hundsun.cprs.yyt.service.system.service.TradeDayService;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class OpenTradeAction extends BaseAction {

    @Autowired
    private ClearingTradeService clearingTradeService;
    @Autowired
    private TradeDayService tradeDayService;

    @RequestMapping("clearing/trade/init")
    public String tradeInit(){
        return "system/trade/init";
    }

    @RequestMapping("open/trade")
    public String openTrade(@RequestParam(value = "tradeDate",required = false) Date tradeDate, ModelMap model){
        try {
            if (tradeDate==null){
                tradeDate = new Date();
            }
            //判断当前时间是否为清算日，如果不是清算日则不进行后面的操作
            if (!tradeDayService.isClearingDate(tradeDate)){
                throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1035.getErrorNo(),EnumBusinessCode.BUSINESS_1035.getErrorInfo());
            }
            clearingTradeService.openTrade(tradeDate);
            return success(model,"开市成功","/system/tradeday/list");
        }catch (Exception e){
            return error(model,"开市失败:" + e.getMessage(),"/system/tradeday/list");
        }
    }

    @RequestMapping("close/trade")
    public String closeTrade(@RequestParam(value = "tradeDate",required = false) Date tradeDate,ModelMap model){
        try {
            if (tradeDate==null){
                tradeDate = new Date();
            }
            //判断当前时间是否为清算日，如果不是清算日则不进行后面的操作
            if (!tradeDayService.isClearingDate(tradeDate)){
                throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1035.getErrorNo(),EnumBusinessCode.BUSINESS_1035.getErrorInfo());
            }
            clearingTradeService.closeTrade(tradeDate);
            return success(model,"闭市成功","/system/tradeday/list");
        }catch (Exception e){
            return error(model,"闭市失败:" + e.getMessage(),"/system/tradeday/list");
        }
    }
}
